/*************************************************************************
	> File Name: test.c
	> Author: 
	> Mail: 
	> Created Time: 2018年11月01日 星期四 19时17分32秒
 ************************************************************************/

#include <stdio.h>
#include "serial.h"
#include <unistd.h>
#include "audio.h"
#include <string.h>
#include <pthread.h>
#include "sqlite.h"
#include <stdlib.h>
#include <error.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "TCPserver.h"
#include "include/QRcode.h"
#define SIZE 1024

int childProcess(int shmid);
int mainProcess(int shmid,struct shmid_ds * shmid_buf,int pid);
char audioBuf[100] = {0};
void *function(void *fd)
{
    int i = *((int *)fd);
    AcceptClient(i);
}

void *threadAudio(void * path)
{
    memset(audioBuf,0,sizeof(audioBuf));
    printf("threadAudio success\r\n");
    while(1)
    {
        if(strlen(audioBuf) <= 0)
        {
            continue;
        }
        AudioPlay(audioBuf);
        memset(audioBuf,0,sizeof(audioBuf));
    }
}

int main()
{
    int shmid;
    char *shmaddr;
    struct shmid_ds shmid_buf;
    int fdServer = InitServer();
    int ret = InitDatbase();
    int flag = 0;
    int pid;
    if(1 == ret)
    {
        printf("InitDatbase error\r\n");
    }
    if(-1 == fdServer)
    {
        printf("InitServer error\r\n");
        return -1;
    }
    pthread_t a_thread;
    ret = pthread_create(&a_thread,NULL,function,(void *)&fdServer);//为网络通信服务创建一个线程
    if(ret != 0)
    {
        printf("function pthread_create error\r\n");
        return -1;
    }
    pthread_t b_thread;
    ret = pthread_create(&b_thread,NULL,threadAudio,NULL);//音乐播放服务
    if(ret != 0)
    {
        printf("threadAudio pthread_create error\r\n");
    }
    //创建共享内存
    shmid = shmget(IPC_PRIVATE,SIZE,IPC_CREAT | 0600);
    if(shmid < 0)
    {
        perror("get shm id error!\r\n");
        return 0;
    }
    pid = fork();
    if(pid == 0)
    {
        childProcess(shmid);
    }
    else if(pid > 0)
    {
        mainProcess(shmid,&shmid_buf,pid);
        
    }
    pthread_join(a_thread,NULL);
    pthread_join(b_thread,NULL);
    return 0;
}

int childProcess(int shmid)
{
    char qrCodeBuf[100] = {0};
    char *shmaddr = (char *)shmat(shmid,NULL,0);
    if(shmaddr == (void *)(-1))
    {
        perror("shmat addr error!\r\n");
        exit(2);
    }
    QRcode(shmaddr);
    return 0;
}


int mainProcess(int shmid,struct shmid_ds * shmid_buf,int pid)
{
    char buf[100] = {0};
    int fd = init_uart();
    int ret = InitDatbase();
    if(ret == 1)
    {
        printf("InitDatbase error\r\n");
    }
    int flag = shmctl(shmid,IPC_STAT,shmid_buf);
    if(flag == -1)
    {
        perror("shmctl error\r\n");
        exit(3);
    }
    printf("shm size = %ld byes \r\n",shmid_buf->shm_segsz);
    printf("parent id = %d,shm_cpid = %d\r\n",getpid(),shmid_buf->shm_cpid);
    printf("child pid = %d,shm_lpid = %d\r\n",pid,shmid_buf->shm_lpid);
    char *shmaddr = (char *)shmat(shmid,NULL,0);
    if(shmaddr == (void *)(-1))
    {
        perror("shmat error \r\n");
        exit(4);
    }
    while(1)
    {
        writecmd(fd,"read_rfid");
        usleep(500000);
        myrecvmsg(fd,buf,sizeof(buf));
        if(strlen(buf) > 10)
        {
            if(!is_valid_card(buf))
            {
                if((ret = is_seniors_card(buf)) == 1)
                {
                    strcpy(audioBuf,"1.wav");
                }
                else
                {
                    strcpy(audioBuf,"2.wav");
                }
                memset(buf,0,sizeof(buf));
                continue;
            }
            strcpy(audioBuf,"3.wav");
            memset(buf,0,sizeof(buf));
        }
        else
        {
            memset(buf,0,sizeof(buf));
        }
        if(strlen(shmaddr) > 0)
        {
            puts(shmaddr);
            if(!is_valid_QRcode(shmaddr))
            {
                strcpy(audioBuf,"1.wav");
                memset(shmaddr,0,sizeof(shmaddr));
                continue;
            }
            strcpy(audioBuf,"3.wav");
            memset(shmaddr,0,sizeof(shmaddr));
        }
    }
}
